# ==========================================
# © 2022 EExuke. All Rights Reserved.
# ==========================================

DIR_OBJ = ./Obj
STARTUP_FILE = ./st_libraries/CMSIS/startup_stm32f10x_hd.s
CONTEXT_FILE = ./rtthread/3.0.3/libcpu/arm/cortex-m3/context_gcc.s
FLASH_LINKER = ./st_libraries/CMSIS/stm32_flash.ld

INC_SRC += -I./st_libraries/CMSIS \
		   -I./st_libraries/STM32F10x_StdPeriph_Driver/inc \
		   -I./rtthread/3.0.3/bsp \
		   -I./rtthread/3.0.3/components/finsh \
		   -I./rtthread/3.0.3/include \
		   -I./rtthread/3.0.3/include/libc \
		   -I./Public \
		   -I./APP/inc \
		   -I./User

DIR_SRC_CMSIS     = ./st_libraries/CMSIS
DIR_SRC_STDDRIVER = ./st_libraries/STM32F10x_StdPeriph_Driver/src
DIR_SRC_RT_BSP    = ./rtthread/3.0.3/bsp
DIR_SRC_RT_FINSH  = ./rtthread/3.0.3/components/finsh
DIR_SRC_RT_COREM3 = ./rtthread/3.0.3/libcpu/arm/cortex-m3
DIR_SRC_RT_SRC    = ./rtthread/3.0.3/src
DIR_SRC_PUBLIC    = ./Public
DIR_SRC_APP_SRC   = ./APP/src
DIR_SRC_USER      = ./User

OBJS += $(patsubst %.c, ${DIR_OBJ}/%.o, $(notdir $(wildcard $(DIR_SRC_CMSIS)/*.c))) \
        $(patsubst %.c, ${DIR_OBJ}/%.o, $(notdir $(wildcard $(DIR_SRC_STDDRIVER)/*.c))) \
        $(patsubst %.c, ${DIR_OBJ}/%.o, $(notdir $(wildcard $(DIR_SRC_RT_BSP)/*.c))) \
        $(patsubst %.c, ${DIR_OBJ}/%.o, $(notdir $(wildcard $(DIR_SRC_RT_COREM3)/*.c))) \
        $(patsubst %.c, ${DIR_OBJ}/%.o, $(notdir $(wildcard $(DIR_SRC_RT_SRC)/*.c))) \
        $(patsubst %.c, ${DIR_OBJ}/%.o, $(notdir $(wildcard $(DIR_SRC_PUBLIC)/*.c))) \
        $(patsubst %.c, ${DIR_OBJ}/%.o, $(notdir $(wildcard $(DIR_SRC_APP_SRC)/*.c))) \
        $(patsubst %.c, ${DIR_OBJ}/%.o, $(notdir $(wildcard $(DIR_SRC_USER)/*.c)))

HEX_TARGET = mainApp.hex
BIN_TARGET = mainApp.bin
ELF_TARGET = ./Obj/mainApp.elf
STARTUP_OBJ = ./Obj/startup_stm32f10x_hd.o
CONTEXT_OBJ = ./Obj/context_gcc.o

C_DEFINES = -DRT_USING_NEWLIB \
			-DHAVE_SYS_SIGNALS

# 使用 arm-none-eabi-gcc
#CC = gcc
CFLAGS = -g -Wall -mthumb -mcpu=cortex-m3 ${C_DEFINES}
ASMFLAGS = -x assembler-with-cpp
TEMP_MAP_FILE = ./Obj/.mainApp.elf.map
LINK_FLAGS = -T ${FLASH_LINKER} -Wl,-cref,-u,Reset_Handler -Wl,-Map=$(TEMP_MAP_FILE) -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 -Wl,--start-group -lc -lm -Wl,--end-group
#LINK_FLAGS = -T ${FLASH_LINKER} -specs=nosys.specs -static -Wl,-cref,-u,Reset_Handler -Wl,-Map=.$(ELF_TARGET).map -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 -Wl,--start-group -lc -lm -Wl,--end-group


hex:
	make ${HEX_TARGET}

bin:
	make ${BIN_TARGET}

${HEX_TARGET}: ${ELF_TARGET}
	$(warning Building $(HEX_TARGET) ...)
	@$(OBJCOPY) $(ELF_TARGET) -O ihex $@

${BIN_TARGET}: ${ELF_TARGET}
	$(warning Building $(BIN_TARGET) ...)
	@$(OBJCOPY) $(ELF_TARGET) -O binary -S $@


${ELF_TARGET}: ${STARTUP_OBJ} ${CONTEXT_OBJ} ${OBJS}
	$(warning Building $(ELF_TARGET) ...)
	@$(CC) $(CFLAGS) ${LINK_FLAGS} $(STARTUP_OBJ) ${CONTEXT_OBJ} $(OBJS) -o $@


${DIR_OBJ}/%.o: ${DIR_SRC_CMSIS}/%.c
	$(warning Comping $< to $@ ...)
	@$(CC) $(CFLAGS) -c $< -o $@ $(INC_SRC);

${DIR_OBJ}/%.o: ${DIR_SRC_STDDRIVER}/%.c
	$(warning Comping $< to $@ ...)
	@$(CC) $(CFLAGS) -c $< -o $@ $(INC_SRC);

${DIR_OBJ}/%.o: ${DIR_SRC_RT_BSP}/%.c
	$(warning Comping $< to $@ ...)
	@$(CC) $(CFLAGS) -c $< -o $@ $(INC_SRC);

${DIR_OBJ}/%.o: ${DIR_SRC_RT_FINSH}/%.c
	$(warning Comping $< to $@ ...)
	@$(CC) $(CFLAGS) -c $< -o $@ $(INC_SRC);

${DIR_OBJ}/%.o: ${DIR_SRC_RT_COREM3}/%.c
	$(warning Comping $< to $@ ...)
	@$(CC) $(CFLAGS) -c $< -o $@ $(INC_SRC);

${DIR_OBJ}/%.o: ${DIR_SRC_RT_SRC}/%.c
	$(warning Comping $< to $@ ...)
	@$(CC) $(CFLAGS) -c $< -o $@ $(INC_SRC);

${DIR_OBJ}/%.o: ${DIR_SRC_PUBLIC}/%.c
	$(warning Comping $< to $@ ...)
	@$(CC) $(CFLAGS) -c $< -o $@ $(INC_SRC);

${DIR_OBJ}/%.o: ${DIR_SRC_APP_SRC}/%.c
	$(warning Comping $< to $@ ...)
	@$(CC) $(CFLAGS) -c $< -o $@ $(INC_SRC);

${DIR_OBJ}/%.o: ${DIR_SRC_USER}/%.c
	$(warning Comping $< to $@ ...)
	@$(CC) $(CFLAGS) -c $< -o $@ $(INC_SRC);


# 编译启动文件
${STARTUP_OBJ}: ${STARTUP_FILE}
	$(warning Comping $< ...)
	@$(CC) $(CFLAGS) $(INC_SRC) -c $< -o $@

# 编译context_gcc文件
${CONTEXT_OBJ}: ${CONTEXT_FILE}
	$(warning Comping $< ...)
	@$(CC) $(CFLAGS) $(INC_SRC) $(ASMFLAGS) -c $< -o $@


# Prevent naming conflicts such as the existence of a file named clean.
.PHONY: clean

clean:
	@find ${DIR_OBJ} -name *.o -exec rm -rf {} \;
	@rm -rf $(BIN_TARGET) ${HEX_TARGET} ${ELF_TARGET}
	@rm -rf $(STARTUP_OBJ)
	@rm -rf $(TEMP_MAP_FILE)

